package com.alan.api.web;

import com.alan.api.service.TestDataService;
import com.alan.api.share.dto.request.TestReq;
import com.alan.api.share.dto.request.TestRequest;
import com.alan.api.util.RedisUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Alan on 17-10-13
 */
@RestController
@RequestMapping("test")
@Validated
public class Test {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    private TestDataService testDataService;

    @RequestMapping("entiy")
    public String entiy(@Valid TestRequest requestBase, BindingResult result){
//        if(result.hasErrors()){
//            List<ObjectError> list = result.getAllErrors();
//            for(ObjectError  error:list){
//                System.out.println(error.getCode()+"---"+error.getArguments()+"---"+error.getDefaultMessage());
//            }
//        }
        System.out.println(JSONObject.toJSON(requestBase));
        return "entiy";
    }

    @RequestMapping("login")
    public String login(@NotNull(message="test not null") String test){
        System.out.println(test);
        return "login"+test;
    }


    @RequestMapping("loginOut")
    public String loginOut(String test){
        System.out.println(test);
        String str = testDataService.testHello(test);
        return "loginOut"+test+";str = "+str;
    }

    @RequestMapping("test2")
    public String test2(TestRequest requestBase){
        System.out.println(JSONObject.toJSON(requestBase));
        return "hello"+JSONObject.toJSON(requestBase);
    }

    @RequestMapping("test3")
    public String test3(TestReq testReq){
        System.out.println(JSONObject.toJSON(testReq));
        return "hello"+JSONObject.toJSON(testReq);
    }

    @RequestMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file,TestReq testReq) throws IOException {
        System.out.println( "name" + file.getOriginalFilename());
        System.out.println(file.getSize());
        InputStream is = file.getInputStream();
        String str = new String( file.getBytes());
        System.out.println("str = "+str);
        if (is != null){
            is.close();
        }
        System.out.println(JSONObject.toJSON(testReq));
        return "hello";
    }

    @RequestMapping("test")
    public String test(String test){
        System.out.println(test);
        return "hello"+test;
    }

    @RequestMapping("ex")
    public String ex(String test){
        Integer.valueOf(test);
        return "hello"+test;
    }

    @RequestMapping("set")
    public String set(String val){
        redisUtils.set("test",val);
        return "ok";
    }

    @RequestMapping("get")
    public String get(String key){
        String str = redisUtils.get(key);
        return "ok" + str;
    }


}
